package com.eastfair.es.util;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.es.vo.AllLogOfEs;
import com.eastfair.es.vo.AllLogSimpleVO;
import com.eastfair.es.vo.AllLogVO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class EsDeatilUtil {

    public static AllLogOfEs assmbleDetail(String message) {
        AllLogOfEs allLogOfEs = new AllLogOfEs();
        if (StringUtils.isNotBlank(message)) {
            JSONObject jsonObject = JSONObject.parseObject(message);
            Integer number = jsonObject.getJSONObject("hits").getJSONObject("total").getInteger("value");
            List<AllLogVO> allLogVOList = new ArrayList<>();
            if (number != null && number > 0) {
                JSONArray jsonArray = jsonObject.getJSONObject("hits").getJSONArray("hits");
                if (jsonArray != null && !jsonArray.isEmpty()) {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        try {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            JSONObject jsonObject2 = jsonObject1.getJSONObject("_source");
                            AllLogVO allLogVO = JSONObject.parseObject(JSONObject.toJSONString(jsonObject2), AllLogVO.class);
                            allLogVO.setHeadParam(jsonObject2.getString("headParam"));
                            allLogVO.setId(jsonObject1.getString("_id"));
                            allLogVOList.add(allLogVO);
                        } catch (Exception e) {

                            e.printStackTrace();
                        }
                    }
                }
            }
            allLogOfEs.setNumber(number);
            allLogOfEs.setAllLogVOList(allLogVOList);
        }
        return allLogOfEs;
    }

    public static List<AllLogSimpleVO> assmbleDetailSimpleStrOfList(List<AllLogVO> allLogVOList) {
        List<AllLogSimpleVO> result = new ArrayList<>();
        if (CollectionUtil.isEmpty(allLogVOList)) {
            return result;
        }
        for (AllLogVO allLogVO : allLogVOList) {
            AllLogSimpleVO allLogSimpleVO = new AllLogSimpleVO();
            allLogSimpleVO.setId(allLogVO.getId());
            allLogSimpleVO.setMsg(assmbleDetailSimpleStr(allLogVO));
            result.add(allLogSimpleVO);
        }
        return result;
    }

    public static String assmbleDetailSimpleStr(AllLogVO allLogVO) {
//        StringBuffer stringBuffer = new StringBuffer();
        //[auth:192.168.200.1:8012:1869275011695251456:admin165177:bfc743fd55314fbab074263427a5965f:]
//        String header = String.format("[%s:%s:%s:%s:%s:%s:]", allLogVO.getAppName(), allLogVO.getServerIp(), allLogVO.getPost(),
//                allLogVO.getUserId(), allLogVO.getTraceId());
        //2023-02-13 10:18:49[ INFO] 20911 [c.e.b.c.handler.AbstractGlobalResponseBodyAdvice.beforeBodyWrite:113] 简化VO功能耗时：2
        String msg = String.format("[%s:%s:%s:%s:%s:%s:] %s[%s][%s.%s.%s]%s",
                allLogVO.getAppName(), allLogVO.getServerIp(), allLogVO.getPost(),
                allLogVO.getUserId(), allLogVO.getUserName(), allLogVO.getTraceId(),
                allLogVO.getDateTimeStr(), allLogVO.getLogLevel(), allLogVO.getClassName(),
                allLogVO.getMethod(), allLogVO.getLine(), allLogVO.getDetail());
        return msg;
    }
}
